﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Связь_с_БД
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
            viewcolumns = new List<string>();
            _dictionaryFull = new Dictionary<string, string>();
        }

        private int _countDict;
        private Dictionary<string, string> _dictionaryFull;
        private List<string> viewcolumns;
        private readonly int[] _numberColumns = {13,16,10,3,6,8,9,4,9,9};
        private bool _isSetChekedBox;

        private void TreeView1NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if(e.Node.Name == "node1")
            {
                tabControl1.Visible = true;
                checkBox1.Visible = true;
                FormMain form = Owner as FormMain;
                if (form != null && !_isSetChekedBox)
                {
                    _dictionaryFull = form.GetDictionaryFull();
                    Dictionary<string, string> dictionaryLoad = new Dictionary<string, string>();
                    GetDictionaryLoadFromFile(dictionaryLoad);
                    SetInCheckedBox(DictionaryToStringArray(_dictionaryFull),dictionaryLoad);
                }
            }
            else
            {
                tabControl1.Visible = false;
                checkBox1.Visible = false;
            }
        }

        private void GetDictionaryLoadFromFile(Dictionary<string,string> dictLoad )
        {
            var sr = File.OpenText(Application.StartupPath + "\\dictLoad.txt");
            while (!sr.EndOfStream)
            {
                AddEntryFromFileLine(dictLoad, sr.ReadLine());
            }
            sr.Close();
        }
        private void AddEntryFromFileLine(Dictionary<string, string> dict, string line)
        {
            string[] fields = new string[2];
            fields[1] = line;
            fields[0] = GetKeyByValue(_dictionaryFull, line);
            dict.Add(fields[0], fields[1]);
        }

        private string GetKeyByValue(Dictionary<string, string> d, string v)
        {
            foreach (string k in d.Keys)
            {
                if (d[k] == v) return k;
            }
            return string.Empty;
        }

        private void Button3Click(object sender, EventArgs e)
        {
            Close();
        }
        private void SettingsFormLoad(object sender, EventArgs e)
        {
            _countDict = 0;
            _isSetChekedBox = false;
        }
        private void SetInCheckedBox(string[] full, Dictionary<string,string> load)
        {
            for (int i = 0; i < _numberColumns[0]; i++)
            {
                if (IsDictionaryContainValue(load,full[i]))
                    checkedListBox1.Items.Add(full[i], true);
                else
                    checkedListBox1.Items.Add(full[i], false);
            }
            checkedListBox1.Visible = true;
            _countDict += _numberColumns[0];

            
            for (int i = _countDict; i < _countDict+_numberColumns[1]; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox2.Items.Add(full[i], true);
                else
                    checkedListBox2.Items.Add(full[i], false);
            }
            checkedListBox2.Visible = true;
            _countDict += _numberColumns[1];

            for (int i = _countDict; i < _countDict + _numberColumns[2]; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox3.Items.Add(full[i], true);
                else
                    checkedListBox3.Items.Add(full[i], false);
            }
            checkedListBox3.Visible = true;
            _countDict += _numberColumns[2];

            for (int i = _countDict; i < _countDict + _numberColumns[3]; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox4.Items.Add(full[i], true);
                else
                    checkedListBox4.Items.Add(full[i], false);
            }
            checkedListBox4.Visible = true;
            _countDict += _numberColumns[3];

            for (int i = _countDict; i < _countDict + _numberColumns[4]; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox5.Items.Add(full[i], true);
                else
                    checkedListBox5.Items.Add(full[i], false);
            }
            checkedListBox5.Visible = true;
            _countDict += _numberColumns[4];

            for (int i = _countDict; i < _countDict + _numberColumns[5]; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox6.Items.Add(full[i], true);
                else
                    checkedListBox6.Items.Add(full[i], false);
            }
            checkedListBox6.Visible = true;
            _countDict += _numberColumns[5];

            for (int i = _countDict; i < _countDict + _numberColumns[6]; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox7.Items.Add(full[i], true);
                else
                    checkedListBox7.Items.Add(full[i], false);
            }
            checkedListBox7.Visible = true;
            _countDict += _numberColumns[6];

            for (int i = _countDict; i < _countDict + _numberColumns[7]; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox8.Items.Add(full[i], true);
                else
                    checkedListBox8.Items.Add(full[i], false);
            }
            checkedListBox8.Visible = true;
            _countDict += _numberColumns[7];

            for (int i = _countDict; i < _countDict + _numberColumns[8]; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox9.Items.Add(full[i], true);
                else
                    checkedListBox9.Items.Add(full[i], false);
            }
            checkedListBox9.Visible = true;
            _countDict += _numberColumns[8];

            for (int i = _countDict; i < _countDict + _numberColumns[9] - 1; i++)
            {
                if (IsDictionaryContainValue(load, full[i]))
                    checkedListBox10.Items.Add(full[i], true);
                else
                    checkedListBox10.Items.Add(full[i], false);
            }
            checkedListBox10.Visible = true;
            _isSetChekedBox = true;
        }

        private string[] DictionaryToStringArray(Dictionary<string,string> dict)
        {
            string[] result = new string[dict.Values.Count];
            for (int i = 0; i < dict.Values.Count - 1;i++ )
            {
                result[i] = dict.Values.ElementAt(i);
            }
            return result;
        }

        private bool IsDictionaryContainValue(Dictionary<string, string> d, string v)
        {
            foreach (string k in d.Keys)
            {
                if (d[k] == v) return true;
            }
            return false;
        }

        private string[] SetNameColumnsForView()
        {
            viewcolumns.Clear();
            foreach (object t in checkedListBox1.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox2.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox3.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox4.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox5.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox6.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox7.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox8.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox9.CheckedItems)
                viewcolumns.Add(t.ToString());
            foreach (object t in checkedListBox10.CheckedItems)
                viewcolumns.Add(t.ToString());
            return viewcolumns.ToArray();
        }

        private void WriteToFileLoadList(string [] cols)
        {
            StreamWriter sw = File.CreateText(Application.StartupPath + "\\dictLoad.txt");
            foreach (string t in cols)
                sw.WriteLine(t);
            sw.Close();
        }

        private void Button1Click(object sender, EventArgs e)
        {
            if(_isSetChekedBox)
            {
                FormMain form = Owner as FormMain;
                if (form != null)
                {
                   form.GetColumnsForView(SetNameColumnsForView());
                   WriteToFileLoadList(viewcolumns.ToArray());
                }
            }
            Close();
        }

        private void CheckBox1CheckedChanged(object sender, EventArgs e)
        {
            SetCheckedInListBox(checkBox1.Checked);
        }

        private void SetCheckedInListBox(bool isChecked)
        {
            for (int i = 0; i < _numberColumns[0]; i++)
                checkedListBox1.SetItemChecked(i,isChecked);
            for (int i = 0; i < _numberColumns[1]; i++)
                checkedListBox2.SetItemChecked(i, isChecked);
            for (int i = 0; i < _numberColumns[2]; i++)
                checkedListBox3.SetItemChecked(i, isChecked);
            for (int i = 0; i < _numberColumns[3]; i++)
                checkedListBox4.SetItemChecked(i, isChecked);
            for (int i = 0; i < _numberColumns[4]; i++)
                checkedListBox5.SetItemChecked(i, isChecked);
            for (int i = 0; i < _numberColumns[5]; i++)
                checkedListBox6.SetItemChecked(i, isChecked);
            for (int i = 0; i < _numberColumns[6]; i++)
                checkedListBox7.SetItemChecked(i, isChecked);
            for (int i = 0; i < _numberColumns[7]; i++)
                checkedListBox8.SetItemChecked(i, isChecked);
            for (int i = 0; i < _numberColumns[8]; i++)
                checkedListBox9.SetItemChecked(i, isChecked);
            for (int i = 0; i < _numberColumns[9]-1; i++)
                checkedListBox10.SetItemChecked(i, isChecked);
        }
    }
}
